<?php

/**
 * uniBoard Template edit
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date$
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_template{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Style API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start Template API
		$this -> api = new api_template( $appCore, $appCore -> _REQ( 'tpl'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_template_not_found'], $appCore -> lang['style_op_templates']);
			
			// Trow user back to groups list
			new action_styles( $appCore);
		}
		else if( $this -> api -> style_id == 1 && !DEVELOPER_MODE)
		{
			// Master error
			$appCore -> output -> error( $appCore -> lang['acp_style_master_templates'], $appCore -> lang['style_op_templates']);
			
			// Trow user back to groups list
			new action_styles( $appCore);
		}
		else
		{
			// Action
			if ( $appCore -> _REQ( 'do') == 'edit')
			{
				// Update
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Fill API with stuff
					$this -> api -> setTemplateGroup( $appCore -> _POST( 'template_group'));
					$this -> api -> setTemplateName( $appCore -> _POST( 'template_name'));
					$this -> api -> setTemplateCode( $appCore -> _POST( 'template_code', false));
					
					// Error?
					if ( $this -> api -> error)
					{
						// Draw error
						$appCore -> output -> error( $this -> api -> last_error, $appCore -> lang['style_op_templates']);
						
						// Repeat Form
						$this -> drawEditForm();
					}
					else
					{
						// Save api
						$this -> api -> saveData();
						
						// Create Log
						$appCore -> logAdmin( 'log_admin_edited_template', htmlspecialchars( $this -> api -> style_name) . ' &raquo; ' . htmlspecialchars( $this -> api -> template_name));
						
						// Redirect back or draw form?
						if ( strlen( $appCore -> _POST( 'leave_editor')) > 0)
						{
							// Redirect
							$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id . '&show=' . urlencode( $this -> api -> template_group)), $appCore -> lang['acp_template_changed']);
						}
						else
						{
							// Draw message
							$appCore -> output -> message( $appCore -> lang['acp_template_changed'], $appCore -> lang['style_op_templates']);
						
							// Repeat form
							$this -> drawEditForm();
						}
					}
				}
				else
				{
					// Draw Form
					$this -> drawEditForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'delete')
			{
				// Delete template
				$this -> api -> deleteData();
				
				// Create Log
				$appCore -> logAdmin( 'log_admin_deleted_template', htmlspecialchars( $this -> api -> style_name) . ' &raquo; ' . htmlspecialchars( $this -> api -> template_name));
						
				// Trow user back to list of templates from our group
				$appCore -> output -> drawRedirect( $appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id . '&show=' . urlencode( $this -> api -> template_group)), $appCore -> lang['acp_template_deleted']);
			}
			else
			{
				// Draw last error
				$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['style_op_templates']);
				
				// Trow user back to groups list
				new action_styles( $appCore);
			}			
		}
		
	}

	// ------------------------------------------------------------------------

	/**
	 * Draw edit form
	 *
	 * @param object $appCore
	 */
	
	function drawEditForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_template&do=edit&tpl=' . $this -> api -> template_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open Block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['template_form_sect_details']);
				
		// Template details
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['template_form_name'], $this -> appCore -> drawTextInput( 'template_name', $this -> api -> template_name), $this -> appCore -> lang['template_form_name_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['template_form_group'], $this -> appCore -> drawTextInput( 'template_group', $this -> api -> template_group), $this -> appCore -> lang['template_form_group_expl']);
		
		// Close Block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Open Block with code
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['template_form_sect_code']);

		// Draw text-area
		echo $this -> appCore -> output -> tableRow( $this -> appCore -> drawBigTextArea( 'template_code', $this -> api -> template_code));
		
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes'], $this -> appCore -> drawButton( 'leave_editor', $this -> appCore -> lang['save_and_leave']));
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
				
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['template_op_edit'], array(
			$this -> appCore -> adminLink( 'act=styles') => $this -> appCore -> lang['acp_styles_list'],
			$this -> appCore -> adminLink( 'act=templates&style=' . $this -> api -> style_id . '&show=' . urlencode(  $this -> api -> template_group)) => htmlspecialchars( $this -> api -> style_name),
			'' => htmlspecialchars( $this -> api -> template_name)
		), ob_get_clean());
		
	}
	
}